package com.example.eshop_ssm.controller;

import com.example.eshop_ssm.annotation.CheckLogin;
import com.example.eshop_ssm.entity.TbBookInfo;
import com.example.eshop_ssm.entity.TbTypeInfo;
import com.example.eshop_ssm.service.AddBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/AddBookController")
public class AddBookController {
    @Autowired
    private AddBookService addBookService;

    /**
     * 跳转页面
     * @throws Exception
     */
    @CheckLogin
    @RequestMapping("/forward")
    public String forward() throws Exception {
        return "addbook";
    }

    /**
     * @return
     * @throws Exception
     */
    @CheckLogin
    @RequestMapping("/selectBook")
    @ResponseBody
    public List<TbTypeInfo> selectBook() throws Exception {
        //一级下拉框
        List<TbTypeInfo> tbTypes = addBookService.selectType();
        return tbTypes;
    }

    /**
     * 查询二级下拉框
     * @param type_id 是一级类型的参数
     * @return
     * @throws Exception
     */
    @CheckLogin
    @RequestMapping("/selectBookTwo")
    @ResponseBody
    public List<TbTypeInfo> selectBookTwo(String type_id) throws Exception {
        //二级下拉框
        List<TbTypeInfo> tbTypes = addBookService.selectTypeTwo(type_id);
        return tbTypes;
    }

    /**
     * 显示要修改的书本信息
     * @param book_id
     * @return
     * @throws Exception
     */
    @CheckLogin
    @RequestMapping("/selectUpdateBook")
    public ModelAndView selectUpdateBook(String book_id) throws Exception {
        //查询要修改的书本信息
        TbBookInfo tbBook = addBookService.selectUpdateBook(book_id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("tbBook", tbBook);
        modelAndView.setViewName("addbook");
        return modelAndView;
    }

    /**
     * 添加书本
     * @param tbBook
     * @return
     * @throws Exception
     */
    @CheckLogin
    @RequestMapping("/addBook")
    @ResponseBody
    public Map<String, Object> addBook(@RequestBody TbBookInfo tbBook) throws Exception {
//        String book_name = req.getParameter("book_name");
//        String type_id = req.getParameter("type_id");
//        String book_price = req.getParameter("book_price");
//        String book_price_old = req.getParameter("book_price_old");
//        String book_author = req.getParameter("book_author");
//        String book_press = req.getParameter("book_press");
//        String book_logo_big = req.getParameter("book_logo_big");
//        String book_logo_small = req.getParameter("book_logo_small");
//        String book_description = req.getParameter("book_description");
//        String store_count = req.getParameter("store_count");
//        String is_new = req.getParameter("is_new");
//        String is_hot = req.getParameter("is_hot");
//        String status = req.getParameter("status");
        System.out.println(tbBook);
        Map<String, Object> stringObjectsMap = addBookService.addBook(tbBook);

        return stringObjectsMap;
    }


    /**
     * 添加子图
     * @param book_id
     * @param picArray
     * @throws Exception
     * @return
     */
    @CheckLogin
    @RequestMapping("/addBookPic")
    @ResponseBody
    public  HashMap<String, Object> addBookPic( String book_id ,  String picArray)throws Exception {
        Integer integer = addBookService.addBookPic(book_id, picArray);
        HashMap<String, Object> effectRow = new HashMap<>();
        effectRow.put("effectRow",integer);
        return effectRow;

    }
}
